package org.example;

public class Test3 {
    //leetcode 删除排序链表中的重复元素II
    //https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/description/?envType=problem-list-v2&envId=linked-list
    public ListNode deleteDuplicates(ListNode head) {
        ListNode newHead = new ListNode(101);
        ListNode slow = newHead, prev = newHead;
        ListNode fast = head;
        while (fast != null) {
            while (fast != null && ((fast.next != null && fast.val == fast.next.val)
                    || fast.val == prev.val)) {
                prev = fast;
                fast = fast.next;
            }
            slow.next = fast;
            prev = fast;
            slow = fast;
            if (fast != null) fast = fast.next;
        }
        return newHead.next;
    }
}
